Welcome back.
In this lecture we're going to be learning a crucial service for scripting any kind of system relating
to user input.
This service is called the User Input Service.
And it contains many functions and events that allow us to listen to a user's input on their local device.
And since this service is purely for user input, it will only work within a local script.
So to get access to this service, we can create a variable and I'm going to call it User Input Service.
And we can go ahead and use the Get Service function and grab the user input service.
And if we index the user input service, there is a plethora of different functions and properties and
events, all relating to user input.
For example, there's many Get functions that allow us to get things such as the mouse's location on
the screen or the mouse delta.
We can get the gamepad state.
We can get current keys that are pressed down.
We can check to see whether or not the player is focused inside of a text box on their screen, and
a bunch of other neat functions.
And of course, we also have a bunch of events in here.
So for example, we can listen to when an input begins or an input ends.
We can listen for touches on the screen or a tap in the world from a mobile device and things of that
nature.
The most important events that we're going to be taking a look at is going to be input began and input
ended.
So I'm going to index for input began.
And then we can go ahead and connect a function to this event.
And as you can see this function gets passed two different arguments.
One is an input object and the other is a boolean.
And this boolean lets us know whether or not this input was observed within a UI element.
So for example, if a player touched a button on their screen, then this game processed event boolean
would return true if a player just tapped randomly on their screen without touching any UI, then this
boolean would be false.
So we can go ahead and define those two parameters.
I'll just call this first parameter input object, and then we can call the second boolean just processed.
And this input object has several different properties that tell us some more information about this
particular input that has began.
For example, if there's a particular key that the player has pressed down on their keyboard, this
property will tell us that we can get the type of the input, whether it's a mouse, a gamepad, if
it's a touch on the screen, and so on.
And then we can also get the state of the user input.
And there's a few other properties like the delta and a position if applicable.
So what we can do here is we can print out whether or not this input was processed.
So I can just print something like was processed by UI.
And then I'll just pass that process.
Boolean.
And then we can check some other things with this input object.
For example, we can check if the input object dot user input type is let's say equal to the enum dot
user input type.
Let's say we want to listen to a left mouse click.
So we can call that mouse button one.
So if this input that has begun is equal to the left mouse button, then that means we know that the
left mouse button is being held down.
So we can put a print statement like mouse was held down.
And of course we can continue on checking for different input.
Maybe, for example, we want to check for a particular key that was pressed.
Then we can check in our input object.
If the key code is equal to, let's say the enum dot key code.
And let's check maybe for the letter e.
So if the key code is E, that means the player has the letter E being pressed down on their keyboard
so we can print something like key E was held down.
And this is for the input began event.
So any time input begins, whether that's the player beginning to press down their mouse button or press
down a key on their keyboard, or if it's a mobile player beginning to touch their screen, this is
the event that's going to fire.
And then we also have inside of the user input service input ended.
And this is going to fire when it says right here when a user stops interacting with their device.
So we can go ahead and connect a function to this as well.
And this function is going to be passed the exact same arguments.
So I'm actually just going to copy all of this.
And then we can just paste that here.
And again we can print whether or not this input ended event was processed by UI.
We can check to see if it was the mouse button, one that got released or the input ended, and we can
go ahead and change.
The statement to mouse was released.
And then the same thing goes for the E button on our keyboard.
We can say key E was released.
So now we can go ahead and playtest our game.
And as you can see, we already got a print statement inside of our console, which is the input began
event.
And that's because as you can see, when my mouse enters and leaves, you can see this event keeps getting
fired.
And that's because it's detecting this new input where the game is going back and focus because my mouse
is inside of the viewport.
And then when my mouse leaves the viewport, as you can see, this event keeps firing.
So that's one way you could use this event, is to detect whether or not the player has entered or left
the viewport with their mouse, but some of the other events I want to go ahead and take a look at is,
for example, clicking.
So if I hold down my left mouse button, as you can see, we got the print statement, mouse was held
down.
And then of course I can click other buttons on my mouse.
For example, I'm clicking a right click right now and as you can see that event keeps firing.
So when I hold it down, it gets printed once.
And then when I release it, it gets printed again because the input began and the input ended.
Event is firing, and then I can go ahead and release my finger off of my left mouse button.
And as you can see, we get mouse was released.
I can go ahead and do the exact same thing for the letter E on my keyboard if I press other keys on
my keyboard.
As you can see, the input began and the input ended event are firing each time a key is pressed on
my keyboard.
So that's why we're getting so many of these print statements in the console.
But if I go ahead and press down E on my keyboard, as you can see, key E was held down and then if
I release it, there we go.
Key E was released.
So the user input service allows us to observe any type of input, whether it's on our keyboard or if
it's with our mouse.
We can also detect touches in our world from a mobile device as well.
So let's go ahead and take a real quick look at how to do that.
For example, in the user input service there is an event such as touch tap.
And if you wanted to detect a tap in the world, then we also have an event for that which is touch
tap in world.
So let's go ahead and check that one out real quick.
We can go ahead and connect a function to this.
And this time we're going to get past two arguments, which is a position where we tapped on a screen,
and the same boolean that tells us whether or not it was processed by UI.
So we can go ahead and get this position.
I'll just call it screen position.
And then we'll just call it processed for the second argument.
And then we can go ahead and print out in our console was processed by UI.
And then we'll just pass our process variable here.
And then we can print out something like uh screen tap at position.
And then we'll just pass our screen position.
Now I'm just going to comment out these other events.
So that way we don't get a whole bunch of these print statements flooding our console.
And then what we can do is we can go to our test tab.
And we can go ahead and do some device testing emulation.
So we're going to change this to device.
Uh, I'll just have the iPad selected and then we can go ahead and play test our game.
And here we are inside of our game.
So if I go ahead and click with my mouse because this is emulated, it's going to fire that touch tap
in world event.
So if I click there we go.
We get our screen tap at position 720 and 367.
So that's defining the exact point where we tapped on our screen in pixels.
So it appears studio is freaking out.
I'm not sure why my mouse is stuck, but that's fine.
As you can see, our event is firing for every time we click on our screen.
Let me see if I can try to fix this and maybe tap somewhere else on the screen real quick.
Okay.
I got my mouse freed up.
If I go ahead and tap something like maybe this button right here.
As you can see, we're getting that same event fired.
But this time the Boolean for that process, Boolean is telling us that it was processed by UI.
And that's because we're clicking the jump button.
But once we stop clicking the jump button and we just click in our world, as you can see, now we're
getting false printed into the console because it's not being processed by any UI on the screen.
For example, if I click my username up here, as you can see that boolean changes to true.
Or if I click up here, that's also true.
And then when I just start clicking randomly inside of the world, that boolean gets set to false.
So the user input service is incredibly useful for listening to specific user inputs on a player's device.
However, there is another service in the Roblox API that makes listening to input extremely simple
and even allows us to support multiple different devices simultaneously without needing to write extra
code to listen to each of those specific devices, like phones, computers, or consoles.
This service is called the Context Action Service, and we'll be taking a look at it in the next lecture.
See you there.